
# Load data - Germany
library(haven)
purpose_de_pooled <- read_dta("~/Desktop/conjointmm/purpose_de_pooled.dta")
View(purpose_de_pooled)

library(rio)
library(gridExtra)
library(ggplot2)
library(cregg)

# conjoint features
tmp <- attr(purpose_de_pooled[["power"]], "label")
purpose_de_pooled[["power"]] <- rio::factorize(purpose_de_pooled[["power"]])
levels(purpose_de_pooled[["power"]]) <- gsub("^.{1}", "???", levels(purpose_de_pooled$power))
attr(purpose_de_pooled[["power"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["purposeR2"]], "label")
purpose_de_pooled[["purposeR2"]] <- rio::factorize(purpose_de_pooled[["purposeR2"]])
attr(purpose_de_pooled[["purposeR2"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["trans"]], "label")
purpose_de_pooled[["trans"]] <- rio::factorize(purpose_de_pooled[["trans"]])
attr(purpose_de_pooled[["trans"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["part"]], "label")
purpose_de_pooled[["part"]] <- rio::factorize(purpose_de_pooled[["part"]])
attr(purpose_de_pooled[["part"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["fair"]], "label")
purpose_de_pooled[["fair"]] <- rio::factorize(purpose_de_pooled[["fair"]])
attr(purpose_de_pooled[["fair"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["problem"]], "label")
purpose_de_pooled[["problem"]] <- rio::factorize(purpose_de_pooled[["problem"]])
attr(purpose_de_pooled[["problem"]], "label") <- tmp
rm(tmp)


# covariates
## internationalist attitudes
tmp <- attr(purpose_de_pooled[["intcoopLOHI"]], "label")
purpose_de_pooled[["Internationalism"]] <- rio::factorize(purpose_de_pooled[["intcoopLOHI"]])
levels(purpose_de_pooled[["Internationalism"]]) <- paste0("Internationalism", levels(purpose_de_pooled[["Internationalism"]]))
attr(purpose_de_pooled[["Internationalism"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["intcoop"]], "label")
purpose_de_pooled[["intcoop"]] <- rio::factorize(purpose_de_pooled[["intcoop"]])
levels(purpose_de_pooled[["intcoop"]]) <- paste0("intcoop: ", levels(purpose_de_pooled[["intcoop"]]))
attr(purpose_de_pooled[["intcoop"]], "label") <- tmp
rm(tmp)

tmp <- attr(purpose_de_pooled[["twolargestparties"]], "label")
purpose_de_pooled[["twolargestparties"]] <- rio::factorize(purpose_de_pooled[["twolargestparties"]])
levels(purpose_de_pooled[["twolargestparties"]]) <- paste0("twolargestparties: ", levels(purpose_de_pooled[["twolargestparties"]]))
attr(purpose_de_pooled[["twolargestparties"]], "label") <- tmp
rm(tmp)

# export
rio::export(purpose_de_pooled, "~/Desktop/purpose_de_pooled.rds")



# calculate quantities for authority and internationalist attitudes

#  Marginal Means
mm <- cj(purpose_de_pooled, confdum ~ power, id = ~ id, weights = ~weight, estimate = "mm", by = ~ intcoop)
mm$statistic <- "MMs"
#  AMCE
amce <- cj(purpose_de_pooled, confdum ~ power, id = ~ id, weights = ~weight, estimate = "amce", by = ~ intcoop)
amce$statistic <- "AMCE"


# AMCE results replication - power purpose trans part fair problem
main <- cj(purpose_de_pooled[!is.na(purpose_de_pooled$Internationalism),], confdum ~ power, id = ~ id, weights = ~weight, estimate = "amce_diff", by = ~ Internationalism)
main$statistic <- "Originally Reported Difference"
## fill in baseline difference
main[5L, ] <-  main[4L, ]
main[5L, "level"] <- levels(purpose_de_pooled$power)[1L]
main[5L, c("estimate", "std.error", "z", "p", "lower", "upper")] <- c(0, rep(NA, 5))

# AMCE differences
amce_diffs <- do.call("rbind", lapply(levels(purpose_de_pooled$power), function(x) {
  tmp <- purpose_de_pooled[!is.na(purpose_de_pooled$Internationalism),]
  tmp$power <- relevel(tmp$power, x)
  cj(tmp, confdum ~ power, id = ~ id, weights = ~weight, estimate = "amce_diff", by = ~ Internationalism)
}))
amce_diffs$statistic <- "Potential Differences in AMCEs"

# Difference in Marginal Means
mm_diff <- cj(purpose_de_pooled, confdum ~ power, id = ~ id, weights = ~weight, estimate = "mm_diff", by = ~ Internationalism)
mm_diff$statistic <- "Difference in MMs"

# Merge diffs
diffs <- rbind(main, amce_diffs, mm_diff)
levels(diffs$statistic) <- c("Originally Reported Difference", "Potential Differences in AMCEs", "Difference in MMs")






# calculate quantities for purpose and partisanship

#  Marginal Means
mm <- cj(purpose_de_pooled, confdum ~ purposeR2, id = ~ id, weights = ~weight, estimate = "mm", by = ~ twolargestparties)
mm$statistic <- "MMs"
#  AMCE
amce <- cj(purpose_de_pooled, confdum ~ purposeR2, id = ~ id, weights = ~weight, estimate = "amce", by = ~ twolargestparties)
amce$statistic <- "AMCE"


# AMCE results replication - purposeR2 purpose trans part fair problem
main <- cj(purpose_de_pooled[!is.na(purpose_de_pooled$twolargestparties),], confdum ~ purposeR2, id = ~ id, weights = ~weight, estimate = "amce_diff", by = ~ twolargestparties)
main$statistic <- "Originally Reported Difference"
## fill in baseline difference
main[5L, ] <-  main[4L, ]
main[5L, "level"] <- levels(purpose_de_pooled$purposeR2)[1L]
main[5L, c("estimate", "std.error", "z", "p", "lower", "upper")] <- c(0, rep(NA, 5))

# AMCE differences
amce_diffs <- do.call("rbind", lapply(levels(purpose_de_pooled$purposeR2), function(x) {
  tmp <- purpose_de_pooled[!is.na(purpose_de_pooled$twolargestparties),]
  tmp$purposeR2 <- relevel(tmp$purposeR2, x)
  cj(tmp, confdum ~ purposeR2, id = ~ id, weights = ~weight, estimate = "amce_diff", by = ~ twolargestparties)
}))
amce_diffs$statistic <- "Potential Differences in AMCEs"

# Difference in Marginal Means
mm_diff <- cj(purpose_de_pooled, confdum ~ purposeR2, id = ~ id, weights = ~weight, estimate = "mm_diff", by = ~ twolargestparties)
mm_diff$statistic <- "Difference in MMs"

# Merge diffs
diffs <- rbind(main, amce_diffs, mm_diff)
levels(diffs$statistic) <- c("Originally Reported Difference", "Potential Differences in AMCEs", "Difference in MMs")
